package cn.com.dhcc.turbo.dao.exch;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.turbo.entity.exch.ExchRel;
import cn.com.dhcc.turbo.entity.exch.NodeLayout;
import cn.com.dhcc.turbo.entity.exch.wapper.ExchRelRowWapper;
import cn.com.dhcc.turbo.entity.exch.wapper.NodeLayoutRowWapper;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.entity.registry.wapper.NodeInfoRowWapper;


/**
 * 数据交换DAO
 * @author admin
 *
 */
@Repository
public class BaseExchDao extends BaseDao {
//	private static Logger logger = Log.getExistLogger(LOGTYPE.TURBO);
	
	
	/**
	 * 判断两个节点之间的关系式
	 * 0 不跨网
	 * 1  跨网
	 * @param localNodeCode
	 * @param destNodeCode
	 * @return
	 */
	public String location(String localNodeCode,String destNodeCode){
		
		return null;
	}
	
	/**
	 * 获取与节点进行数据交换的全部子节点列表 包含有关系节点和管理范围内的节点
	 * @param nodeId 当前节点ID
	 * @return
	 * @throws DaoException
	 */
	public List<NodeInfo> getAllExchNodeList(String nodeId) throws DaoException {
		try {
			 final String sql =  "select distinct t2.*"
	 					+ " from " + SI_TABLE("turbo_exch_rel") + " t1, " + SI_TABLE("turbo_node_info") + " t2"
						+ "	 where (t1.node_id_creator = t2.id or t1.node_id_to = t2.id)"
						+ "	   and (t1.node_id_creator = ?"
						+ "	    or t1.node_id_to = ? or t2.pid=?)";
			return CM.getDao().list("获取与本节点进行数据交换的节点列表", sql, new NodeInfoRowWapper(),
					new Object[] {nodeId,nodeId,nodeId});
		} catch (DaoException e) {
			throw new DaoException("获取与本节点进行数据交换的节点列表失败！", e);
		}
	}
	/**
	 * 通过节点id返回交换关系
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public List<ExchRel> getAllExchByNodeId(String nodeId) throws DaoException {
		try {
			 final String sql =  "select *  from " + SI_TABLE("turbo_exch_rel") 
						+ "	 where node_id_creator = ? or node_id_to = ? ";
			return CM.getDao().list("通过节点id返回交换关系", sql, new ExchRelRowWapper(),
					new Object[] {nodeId,nodeId});
		} catch (DaoException e) {
			throw new DaoException("通过节点id返回交换关系失败！", e);
		}
	}
	/**
	 * 通过节点id返回交换关系图
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public NodeLayout getNodeLayout(String nodeId) throws DaoException {
		final String sql = " select * from  " + SI_TABLE("turbo_node_layout") +" where node_id=?";
		try {
		return CM.getDao().get("通过节点id返回交换关系图", sql, new NodeLayoutRowWapper(),
				new Object[] {nodeId});
		} catch (DaoException e) {
			throw new DaoException("返回交换关系图失败！", e);
		}
	}
	/**
	 * 建立交换关系
	 * @param vo
	 * @throws DaoException
	 */
	public void add(ExchRel vo)  throws DaoException {
		try {
			CM.getDao().insertModule("建立交换关系", vo);
		} catch (DaoException e) {
			throw new DaoException("建立交换关系失败！", e);
		}
	}
	/**
	 * 删除交换关系
	 * @param creatorId
	 * @param toId
	 * @throws DaoException
	 */
	public void delete(String creatorId,String toId)  throws DaoException {
		final String sql = " delete from  " + SI_TABLE("turbo_exch_rel") +" where (node_id_creator = ? or node_id_to=?) and (node_id_creator = ? or node_id_to=?)";
		try {
			CM.getDao().delete("删除交换关系", sql, new Object[] {creatorId,creatorId,toId,toId});
		} catch (DaoException e) {
			throw new DaoException("删除交换关系失败！", e);
		}
	}
	/**
	 * 保存交换关系图
	 * @param vo
	 * @throws DaoException
	 */
	public void addNodeLayout(NodeLayout vo)  throws DaoException {
		final String sql = " insert into " + SI_TABLE("turbo_node_layout") +" (id,node_id,rel_node_layout) values(?,?,?)";
		try {
			CM.getDao().insert("保存交换关系图", sql, new Object[] {vo.getId(),vo.getNodeId(),vo.getRelNodeLayout()});
		} catch (DaoException e) {
			throw new DaoException("保存交换关系图失败！", e);
		}
		
	}
	public void delExchRelByNodeId(String nodeId) throws DaoException {
		final String sql = " delete from  " + SI_TABLE("turbo_exch_rel") +" where node_id_creator = ? or  node_id_to=?";
		try {
			CM.getDao().delete("删除交换关系", sql, new Object[] {nodeId,nodeId});
		} catch (DaoException e) {
			throw new DaoException("删除交换关系失败！", e);
		}
		
	}
	public void updateNodeLayout(NodeLayout vo) throws DaoException {
		final String sql = " update   " + SI_TABLE("turbo_node_layout") +" set rel_node_layout=? where node_id = ?";
		try {
			CM.getDao().update("修改交换关系", sql, new Object[] {vo.getRelNodeLayout(),vo.getNodeId()});
		} catch (DaoException e) {
			throw new DaoException("修改交换关系失败！", e);
		}
	}
	/**
	 * 验证交换关系
	 * @param fromNodeId
	 * @param toNodeId
	 * @return
	 * @throws DaoException
	 */
	public ExchRel check(String fromNodeId, String toNodeId) throws DaoException {
		final String sql = " select * from  " + SI_TABLE("turbo_exch_rel") +" t"
									+"  where (t.node_id_creator = ? or t.node_id_to = ?)"
									+"  and (t.node_id_creator = ? or  t.node_id_to =?)";
		try {
			return CM.getDao().get("通过节点id返回交换关系图", sql, new ExchRelRowWapper(),new Object[] {fromNodeId,fromNodeId,toNodeId,toNodeId});
		} catch (DaoException e) {
			throw new DaoException("返回交换关系图失败！", e);
		}
		
	}
	/**
	 * 获得所有交换关系
	 * @return
	 * @throws DaoException
	 */
	public List<ExchRel> getAllExchRel() throws DaoException {
		final String sql = " select * from  " + SI_TABLE("turbo_exch_rel");
		try {
		return CM.getDao().list("获得所有交换关系", sql, new ExchRelRowWapper(),new Object[] {});
		} catch (DaoException e) {
			throw new DaoException("获得所有交换关系失败！", e);
		}
	}
}
